2
2
.
.
9
9
.
.
1
1
M
M
a
a
n
n
u
u
a
a
l
l
l
l
y
y
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to manually instantiate a Class without using @Autowired.
This is useful to know to get better understanding of what is Spring Boot doing in the background.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller and @RequestMapping. Includes Tomcat Server.
MyService
http://localhost:8080/Hello
Tomcat
Browser
MyController
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_autowired_manually (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
Create Package: services (inside main package)
– Create Class: MyService.java (inside package controllers)
MyController.java
package com.ivoronline.springboot_autowired_manually.controllers;
import com.ivoronline.springboot_autowired_manually.services.MyService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
//@Autowired
MyService myService = new MyService();
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
String Results = myService.sayHello();
return Results;
}
}
MyService.java
package com.ivoronline.springboot_autowired_manually.services;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public String sayHello() {
return "Hello";
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>